/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.fedoraaccess.metadataUpdate.datastream;

import cz.mzk.kramerius.commons.DatastreamId;
import cz.mzk.kramerius.commons.Dom4jUtils;
import cz.mzk.kramerius.commons.Model;
import cz.mzk.kramerius.fedoraaccess.FedoraConnector;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class DcTypeModelUpdater extends InlineXmlDatastreamUpdater {

    private final XPath typeXpath = Dom4jUtils.createXPath("//dc:type");
    private final Model model;

    public DcTypeModelUpdater(FedoraConnector con, Model model) {
        super(con, DatastreamId.DC);
        this.model = model;
    }

    @Override
    public Document updateDatastreamDoc(Document originalDs) {
        List<Element> elements = typeXpath.selectNodes(originalDs);
        for (Element element : elements) {
            String text = element.getText();
            if (text.startsWith("model:")) {
                element.setText("model:" + model.toString());
            }
        }
        return originalDs;
    }
}
